//index.js
//获取应用实例
const app = getApp()
var touchDot = 0;//触摸时的原点 
var time = 0;// 时间记录，用于滑动时且时间小于1s则执行左右滑动 
var interval = "";// 记录/清理时间记录 
Page({
  data: {
    hidden:true,
    pltype: false,
    pltype1: true,
    pltype2: false,
    sharetype:false,
    addfocus: true,
    addfocus1: false,
    addxa: true,
    addxa1: false,
    addxa2: true,
    addxa3: false,
    addxa4: true,
    addxa5: false,
    addSouc:true,
    addSouc1:false,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    playtype:'1',
    frameClass1: 'frame z1',//默认正面在上面
    frameClass2: 'frame z2',
    imgUrls: [
      '../../images/111.jpeg',
      '../../images/banner.png',
      '../../images/banner.png',
      '../../images/banner.png'
    ],
    tabArr: {
      curHdIndex: 0,
      curBdIndex: 0
    },
    demoData: [

    ],
    eye: true,
    poster: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000',
    name: '此时此刻',
    author: '许巍',
    src: 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46',
    swiperIndex: 0, //这里不写第一次启动展示的时候会有问题
    swiperCurrent: 1,
    arr: [{
      images: '../../images/111.jpeg'
    },
    {
      images: '../../images/banner.png'
    },
    {
      images: '../../images/banner.png'
    },
    {
      images: '../../images/banner.png'
    }
    ],
    indicatorDots: false,
    autoplay: false,
    interval: 2000,
    duration: 1000,
    circular: true,
    jiazai:true,
    beforeColor: "white",//指示点颜色  
    afterColor: "coral",//当前选中的指示点颜色  
    previousmargin: '15px',//前边距
    nextmargin: '15px',//后边距
  },
  onReady: function (e) {
    // 使用 wx.createAudioContext 获取 audio 上下文 context
    this.audioCtx = wx.createAudioContext('myAudio');
    
  },
  //轮播图的切换事件  
  swiperChange: function (e) {
    console.log(e.detail.current);
    this.setData({
      swiperCurrent: e.detail.current   //获取当前轮播图片的下标
    })
  },
  //滑动图片切换  
  chuangEvent: function (e) {
    this.setData({
      swiperCurrent: e.currentTarget.id
    })
  },
  commentBtn: function (e) {
    console.log(e.detail.value);
    var zhuijia = { demoCon: e.detail.value };
    this.data.demoData.push(zhuijia);
    this.setData({
      demoData: this.data.demoData,
      pltype1:false,
      pltype2:true
    });
  }
  ,
  tabFun: function (e) {
    //获取触发事件组件的dataset属性
    var _datasetId = e.target.dataset.id;
    console.log("----" + _datasetId + "----");
    var _obj = {};
    _obj.curHdIndex = _datasetId;
    _obj.curBdIndex = _datasetId;
    this.setData({
      tabArr: _obj
    });
  },
  rotateFn: function (e) {
    var that = this
    if (this.data.frameClass1 == 'frame z1' &&
      this.data.frameClass2 == 'frame z2') {
      that.setData({
        frameClass1: "frame front",
        frameClass2: "frame back",
      })
      setTimeout(function () {
        that.setData({
          frameClass1: "frame z2",
          frameClass2: "frame z1",
        })
      }, 1000);
    }
    else {
      that.setData({
        frameClass1: "frame back",
        frameClass2: "frame front",
      })
      setTimeout(function () {
        that.setData({
          frameClass1: "frame z1",
          frameClass2: "frame z2",
        })
      }, 1000);
    }
  },
  audioPlay: function () {
    if (this.data.playtype==1){
      this.audioCtx.play();
      this.setData({
        playtype: '2'
      })
    }else{
      this.audioCtx.pause();
      this.setData({
        playtype: '1'
      })
    }
    
  },
  openshare:function(e){
    var that=this;
    that.setData({
      sharetype:true
    })
  },
  colseshare: function (e) {
    var that = this;
    that.setData({
      sharetype: false
    })
  },
  openpl: function (e) {
    var that = this;
    that.setData({
      pltype: true
    })
  },
  closepl: function (e) {
    var that = this;
    that.setData({
      pltype: false
    })
  },
  bindchange(e) {
    this.setData({
      swiperIndex: e.detail.current
    })
    console.log(1);
  },
  addFocus:function(){
    this.setData({
      addfocus: false,
      addfocus1: true,
    })
    wx.showToast({
      title: '关注成功',
      icon: "none",
      duration: 1500,
      mask: true
    })
  },
  addFocus1: function () {
    this.setData({
      addfocus: true,
      addfocus1: false,
    })
    wx.showToast({
      title: '取消关注',
      icon: "none",
      duration: 1500,
      mask: true
    })
  },
  addSouc: function () {
    this.setData({
      addSouc: false,
      addSouc1: true,
    })
    // wx.showToast({
    //   title: '收藏成功',
    //   icon: "none",
    //   duration: 1500,
    //   mask: true
    // })
  },
  addSouc1: function () {
    this.setData({
      addSouc: true,
      addSouc1: false,
    })
    // wx.showToast({
    //   title: '取消关注',
    //   icon: "none",
    //   duration: 1500,
    //   mask: true
    // })
  },
  addxa: function () {
    this.setData({
      addxa: false,
      addxa1: true,
    })
  },
  addxa1: function () {
    this.setData({
      addxa: true,
      addxa1: false,
    })
  },
  addxa2: function () {
    this.setData({
      addxa2: false,
      addxa3: true,
    })
  },
  addxa3: function () {
    this.setData({
      addxa2: true,
      addxa3: false,
    })
  },
  addxa4: function () {
    this.setData({
      addxa4: false,
      addxa5: true,
    })
  },
  addxa5: function () {
    this.setData({
      addxa4: true,
      addxa5: false,
    })
  },
  goDetails:function(e){
    console.log(e.currentTarget.dataset.url);
    wx.navigateTo({
      url: e.currentTarget.dataset.url
    })
  },
  rotateFns:function(){
    wx.navigateTo({
      url: '../zuoPinDetails/zuoPinDetails'
    })
  },
  goAuthor:function(){
    wx.navigateTo({
      url: '../author/author'
    })
  },
  goManyi:function(e){
    console.log(e.currentTarget.dataset.url);
    wx.redirectTo({
      url: e.currentTarget.dataset.url
    })
  },
  // 触摸开始事件 
  touchStart: function (e) {
    touchDot = e.touches[0].pageY; // 获取触摸时的原点 
    // 使用js计时器记录时间  
    interval = setInterval(function () {
      time++;
    }, 100);
  },
  // 触摸移动事件 
  touchMove: function (e) {
    var touchMove = e.touches[0].pageY;
    console.log("touchMove:" + touchMove + " touchDot:" + touchDot + " diff:" + (touchMove - touchDot));
    // 向xia滑动  
    if (this.data.jiazai){
      if (touchMove - touchDot <= -17 && time < 10) {
        wx.navigateTo({
          url: '../zuoPinDetails/zuoPinDetails'
        });

      }
      this.setData({
        jiazai:false
      })
    }else{
      console.log(1);
    }
    
    // 向右滑动 
    // if (touchMove - touchDot >= 40 && time < 10) {
    //   console.log('向右滑动');
    //   wx.switchTab({
    //     url: '../右滑页面/右滑页面'
    //   });
    // }
  },
  // 触摸结束事件 
  touchEnd: function (e) {
    clearInterval(interval); // 清除setInterval 
    time = 0;
    this.setData({
      jiazai: true
    })
  },
  onShow: function (options){
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  bindGetUserInfo: function (e) {
    console.log(e.detail.userInfo);
    console.log(1);
    wx.setStorageSync("userInfo", e.detail.userInfo);
    if (e.detail.userInfo== undefined){
      wx.redirectTo({
          url: '../my/my'
        })
    }else{
      wx.redirectTo({
          url: '../my/my'
        })
    }
   
  },
  goMy: function () {
    // var that = this;
    // var userInfo = wx.getStorageSync("userInfo");
    // console.log(userInfo)
    // var userid = wx.getStorageSync("userid");
    // console.log(userid)
    // if (userInfo != "") {
    //   // if (userid == "" || userid == null || userid == undefined) {
    //   //   wx.navigateTo({
    //   //     url: '../gocz/gocz?type=1'
    //   //   })
    //   // } else {
    //     wx.redirectTo({
    //       url: '../my/my'
    //     })
    //   // }

    // } else {
    //   wx.getSetting({
    //     success: function (res) {
    //       if (res.authSetting['scope.userInfo']) {
    //         // 已经授权，可以直接调用 getUserInfo 获取头像昵称
    //         wx.getUserInfo({
    //           success: function (res) {
    //             // console(res.userInfo);
    //             wx.setStorageSync("userInfo", res.userInfo);
    //           }
    //         })


    //       }
         
    //         wx.redirectTo({
    //           url: '../my/my'
    //         })

    //     }
    //   })

    // }


  },
 
})
